package com.joekai.view_demo.message_drag;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.drawable.AnimationDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.joekai.view_demo.R;
import com.joekai.view_demo.utils.BubbleUtils;

public class MessageBubbleTouchListener implements View.OnTouchListener, MessageDragListener {
    private View mStaticView;
    private WindowManager mWindowManager;
    private MessageDragView mMessageBubbleView;
    private WindowManager.LayoutParams mParams;
    // 爆炸动画
    private FrameLayout mBombFrame;
    private ImageView mBombImage;
    private DragDisappearListener mBubbleDisappearListener;

    public MessageBubbleTouchListener(View view, Context context, DragDisappearListener disappearListener){
        mStaticView = view;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mMessageBubbleView = new MessageDragView(context);
        mMessageBubbleView.setMessageBubbleListener(this);

        mParams = new WindowManager.LayoutParams();
        mParams.format = PixelFormat.TRANSLUCENT;

        mBombFrame = new FrameLayout(context);
        mBombImage = new ImageView(context);
        mBombImage.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mBombFrame.addView(mBombImage);
        mBubbleDisappearListener = disappearListener;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //将自己添加到WindowManager上
                mWindowManager.addView(mMessageBubbleView, mParams);
                //初始化贝塞尔点
                mMessageBubbleView.initPoint(event.getRawX(), event.getRawY() - BubbleUtils.getStatusBarHeight());
                mMessageBubbleView.setDragBitmap(getViewBitmap(mStaticView));
                //按下时，隐藏自己
                mStaticView.setVisibility(View.INVISIBLE);
                break;
            case MotionEvent.ACTION_MOVE:
                mMessageBubbleView.updateDragPoint(event.getRawX(), event.getRawY() - BubbleUtils.getStatusBarHeight());
                break;
            case MotionEvent.ACTION_UP:
                mMessageBubbleView.handleActionUp();
                break;
        }
        return true;
    }
    private Bitmap getViewBitmap(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;

    }

    @Override
    public void restore() {
        // 把消息的View移除
        mWindowManager.removeView(mMessageBubbleView);
        // 把原来的View显示
        mStaticView.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismiss(PointF pointF) {
        // 要去执行爆炸动画 （帧动画）
        // 原来的View的View肯定要移除
        mWindowManager.removeView(mMessageBubbleView);
        // 要在 mWindowManager 添加一个爆炸动画
        mWindowManager.addView(mBombFrame, mParams);
        mBombImage.setBackgroundResource(R.drawable.anim_bubble_pop);

        AnimationDrawable drawable = (AnimationDrawable) mBombImage.getBackground();
        mBombImage.setX(pointF.x - drawable.getIntrinsicWidth() / 2);
        mBombImage.setY(pointF.y - drawable.getIntrinsicHeight() / 2);

        drawable.start();
        // 等它执行完之后我要移除掉这个 爆炸动画也就是 mBombFrame
        mBombImage.postDelayed(new Runnable() {
            @Override
            public void run() {
                mWindowManager.removeView(mBombFrame);
                // 通知一下外面该消失
                if (mBubbleDisappearListener != null) {
                    mBubbleDisappearListener.dismiss(mStaticView);
                }
            }
        }, getAnimationDrawableTime(drawable));
    }
    private long getAnimationDrawableTime(AnimationDrawable drawable) {
        int numberOfFrames = drawable.getNumberOfFrames();
        long time = 0;
        for (int i = 0; i < numberOfFrames; i++) {
            time += drawable.getDuration(i);
        }
        return time;
    }
}
